using System;
using System.Collections.Generic;
using System.Text;
using PetShop.Infrastructure.Gateways;
using PetShop.Domain.Model;

namespace PetShop.Domain.Services
{
    public class CartServices
    {

        private readonly ItemGateway _gateway;

        public CartServices()
        {
            _gateway = new ItemGateway();
        }
        
        public CartInfo AddItemToCart(CartInfo cart, string ItemId)
        {


            //if there is already a cartitem for this item then augment the quantity with one
            if (cart.Exists(ItemId))
            {
                foreach (CartItemInfo cartItem in cart.Items)
                {
                    if (ItemId == cartItem.Item.Id)
                    {
                        cartItem.Quantity++;
                        decimal totalItem = cartItem.Price * cartItem.Quantity;
                        cart.Total = cart.Total + totalItem;
                        return cart;
                    }
                }
            }
            else
            {
              
                ItemEntity item = _gateway.GetItemByID(ItemId);
                CartItemInfo newItem = new CartItemInfo(item, 1);
                cart.Add(newItem);
            }

            return cart;


        }

    }
}
